package com.wln.pms.pms.controller;

import java.util.Arrays;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.wln.pms.pms.conf.OssUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.wln.pms.pms.entity.GoodsEntity;
import com.wln.pms.pms.service.GoodsService;
import com.wln.common.utils.PageUtils;
import com.wln.common.utils.R;
import org.springframework.web.multipart.MultipartFile;


/**
 * 
 *
 * @author wulinna
 * @email 2258732595@qq.com
 * @date 2022-02-22 14:27:49
 */
@RestController
@RequestMapping("pms/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @Autowired
    private OssUtils ossUtils;

    @RequestMapping("getByGoodsId")
    public R getByGoodsId(@RequestParam("goodsId") Long goodsId){
        GoodsEntity byId = goodsService.getById(goodsId);
//        String s = JSON.toJSONString(byId);
        return R.ok().setData(byId);
    }

    @RequestMapping("/updateByGoodsId")
    public R updateByGoodsId(@RequestParam("goodsId") Long s,@RequestParam("goodsCount") Integer goodsCount){
        GoodsEntity byId = goodsService.getById(s);
        byId.setGoodsStock(byId.getGoodsStock()-goodsCount);
        goodsService.updateById(byId);
        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = goodsService.queryPage(params);

        return R.ok().setData(page.getList());
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{goodsId}")
    public R info(@PathVariable("goodsId") Long goodsId){
		GoodsEntity goods = goodsService.getById(goodsId);

        return R.ok().put("goods", goods);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody GoodsEntity goods){
		goodsService.save(goods);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody GoodsEntity goods){
		goodsService.updateById(goods);

        return R.ok();
    }

    /**
     * 上传图片
     */
    @RequestMapping("/upload")
    public R upload(MultipartFile file) {
        R r = ossUtils.uploadFile(file);
        return r;
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] goodsIds){
		goodsService.removeByIds(Arrays.asList(goodsIds));

        return R.ok();
    }

}
